
<script type="text/javascript">
	dojo.require("dijit.form.Button");
	dojo.require("dojo.data.ItemFileReadStore");
	dojo.require("dojo.parser");
	dojo.require("dojo.fx");
  
  	//Datastores
	var raidStore = new dojo.data.ItemFileReadStore({url:"<?=$this->url(array('controller'=>'raids', 'action'=>'getraid')) . "?raidId=" . $this->raidId?>"});
	var raidDungeonsStore = new dojo.data.ItemFileReadStore({url:"<?=$this->url(array('controller'=>'raids', 'action'=>'getraiddungeons')) . "?raidId=" . $this->raidId?>"});
	var signupStore;//@TODO all data stores should be made like this :)
	var charactersStore;

	var priority;

	function doNothing() {
	    alert('not actually doing anything, just a test!');
	  }
	
	var updateRaid = function(items, request){
	  	for (var i = 0; i < items.length; i++){
	  		var item = items[i];
	  		priority = raidStore.getValue(item,'priority');
	  		dojo.byId("start_time").innerHTML = raidStore.getValue(item,'start_time');
	  		dojo.byId("finish_time").innerHTML = raidStore.getValue(item,'finish_time');
	  		dojo.byId("signup_deadline").innerHTML = raidStore.getValue(item,'signup_deadline');
	  		dojo.byId("raidleader").innerHTML = raidStore.getValue(item,'raidleader_name');
	  	}
	};
	
	var updateRaidDungeons = function(items, request){
		for (var i = 0; i < items.length; i++){
			var item = items[i];	  		
			var div = document.createElement("div");
			div.innerHTML = raidDungeonsStore.getLabel(item);
			dojo.byId("dungeons").appendChild(div);
		}
	};

	var updateSignups = function(items, request){
		for (var i = 0; i < items.length; i++){
			var item = items[i];
			
			//creating a div for the signup
			var div = document.createElement("div");
			//setting attributes
			dojo.attr(div,'id','user_'+signupStore.getValue(item,"user_id"));
			dojo.attr(div,'roll',signupStore.getValue(item,"roll"));
			dojo.attr(div,'rank_id',signupStore.getValue(item,"role_rank"));
			dojo.attr(div,'status',signupStore.getValue(item,"status"));
			dojo.attr(div,'class_id',signupStore.getValue(item,"class_id"));

			//Setting content (layout!!!)
			var charName = document.createElement("div");
			dojo.attr(charName,'id','char_'+signupStore.getValue(item,"character_id"));
			charName.innerHTML = signupStore.getValue(item,"character_name");


			//changing class of name container depending on raider rank
			if (dojo.attr(div,'rank_id') < priority) dojo.attr(div,'class','lowrank');
			else if (dojo.attr(div,'rank_id') == priority) dojo.attr(div,'class','exactrank'); 
			else dojo.attr(div,'class','uberrank');

			//Writting out the rest
			var theRest = document.createElement("div");
			dojo.attr(theRest,'id','user_'+signupStore.getValue(item,"user_id")+'rest');
			theRest.innerHTML = signupStore.getValue(item,"role_name") + " " + signupStore.getValue(item,"roll") + " " +
			signupStore.getValue(item,"note");	

			div.appendChild(charName);
			div.appendChild(theRest);
			placeSignup(div);			
		}
	};

	var placeSignup = function(div){
		if (dojo.attr(div,'status') == 1) dojo.byId('class_'+dojo.attr(div,'class_id')+'_signed').appendChild(div);
		else if (dojo.attr(div,'status') == 2) dojo.byId('class_'+dojo.attr(div,'class_id')+'_confirmed').appendChild(div);
		else if (dojo.attr(div,'status') == 3) dojo.byId('class_'+dojo.attr(div,'class_id')+'_unsigned').appendChild(div);
		else if (dojo.attr(div,'status') == 4) dojo.byId('class_'+dojo.attr(div,'class_id')+'_signed').appendChild(div);//unsign after confirm
	};

	var adminSign = function(userId,status){
		data['raidId'] = <?=$this->raidId?>;
		data['userId'] = userId;
		data['status'] = status;
		dojo.xhrPost({
			url:"<?= $this->url(array('controller'=>'raids', 'action'=>'adminsign')) ?>",
			content: data,
			handle: function(cbData,args){
				if (cbData == 'ok'){
					refreshSignups();
				} else alert(cbData);
			}
		});
	};

	
	var unsign = function(characterId){
		var data = new Object();
		data['raidId'] = <?=$this->raidId?>;
		dojo.xhrPost({
			url:"<?= $this->url(array('controller'=>'raids', 'action'=>'unsign')) ?>",
			content: data,
			handle: function(cbData,args){
				if (cbData == 'ok'){
					refreshSignups();
				} else alert(cbData);
			}
		});
	};


	var signup = function(characterId){
		var data = new Object();
		data['charId'] = characterId;
		data['raidId'] = <?=$this->raidId?>;
		data['role'] = document.roles['char'+characterId+'roles'].value;
		data['note'] = document.roles['char'+characterId+'note'].value;
		
		dojo.xhrPost({
			url:"<?= $this->url(array('controller'=>'raids', 'action'=>'signup')) ?>",
			content: data,
			handle: function(cbData,args){
				if (cbData == 'ok'){
					refreshSignups();
				} else alert(cbData);
			}
		});
	};

	var updateCharactersStore = function(){
		dojo.xhrPost({
			url:"<?= $this->url(array('controller'=>'characters', 'action'=>'getcharacters')) ?>",
			handleAs: "json",
			handle: function(cbData,args){
					charactersStore = new dojo.data.ItemFileReadStore({data: cbData});
					charactersStore.fetch({onComplete: updateDefaults});
			}
		});
	};

	var refreshSignups = function(){
		var data = new Object();
		data['raidId'] = <?=$this->raidId?>;
		dojo.xhrPost({
			url:"<?= $this->url(array('controller'=>'raids', 'action'=>'getsignups')) ?>",
			content: data,
			handleAs: "json",
			handle: function(cbData,args){
					signupStore = new dojo.data.ItemFileReadStore({data: cbData});
					clearSignups();
					signupStore.fetch({onComplete: updateSignups});
			}
		});
	};

	var clearSignups = function(){
		for (var i = 1; i <= 10; i++){
			 document.getElementById("class_"+i+"_confirmed").innerHTML = "";
			 document.getElementById("class_"+i+"_signed").innerHTML = "";
			 document.getElementById("class_"+i+"_unsigned").innerHTML = "";			 
		}
	};

	var updateDefaults = function(items, request){
		for (var i = 0; i < items.length; i++){
			var item = items[i];
			characterId = charactersStore.getValue(item,'id');
			document.roles['char'+characterId+'roles'].value = charactersStore.getValue(item,'default_role');
			document.roles['char'+characterId+'note'].value = charactersStore.getValue(item,'default_note');
		}
	};


  	
	var initDND = function()
	{
		raidStore.fetch({onComplete: updateRaid}); 
		raidDungeonsStore.fetch({onComplete: updateRaidDungeons});
		refreshSignups();
		updateCharactersStore();	
	}; 

	dojo.addOnLoad(initDND);
	   
</script>







<h1>Raid Info:</h1h1>
<div style="float: left">
<div><b>Raid Leader:</b> <span id="raidleader"></span> </div>
<div><b>Raid Start Time:</b> <span id="start_time"></span> </div>
<div><b>Raid Finish Time:</b> <span id="finish_time"></span> </div>
<div><b>Raid Sign-up Deadline:</b> <span id="signup_deadline"></span> </div>

</div>
<div style="float: right">
<div><b>Dungeon(s):</b> <div id="dungeons"></div> </div>
</div>

<div style="clear: both"></div>
<h1>Confirmed:</h1>
<table class="shaded" style="width : 100%"> 
	<tr>
		<th>Death Knight</th>
		<th>Druid</th>
		<th>Hunter</th>
		<th>Mage</th>
		<th>Paladin</th>
		<th>Priest</th>
		<th>Rogue</th>
		<th>Shaman</th>
		<th>Warlock</th>
		<th>Warrior</th>		
	</tr>
	<tr>
		<td>
			<div id="class_1_confirmed"></div>
		</td>
		<td>
			<div id="class_2_confirmed"></div>
		</td>
		<td>
			<div id="class_3_confirmed"></div>
		</td>
		<td>
			<div id="class_4_confirmed"></div>
		</td>
		<td>
			<div id="class_5_confirmed"></div>
		</td>
		<td>
			<div id="class_6_confirmed"></div>
		</td>
		<td>
			<div id="class_7_confirmed"></div>
		</td>
		<td>
			<div id="class_8_confirmed"></div>
		</td>
		<td>
			<div id="class_9_confirmed"></div>
		</td>
		<td>
			<div id="class_10_confirmed"></div>
		</td>		
	</tr>
</table>

<div style="clear: both"></div>
<h1>Signed:</h1>
<table class="shaded" style="width : 100%"> 
	<tr>
		<th>Death Knight</th>
		<th>Druid</th>
		<th>Hunter</th>
		<th>Mage</th>
		<th>Paladin</th>
		<th>Priest</th>
		<th>Rogue</th>
		<th>Shaman</th>
		<th>Warlock</th>
		<th>Warrior</th>		
	</tr>
	<tr>
		<td>
			<div id="class_1_signed"></div>
		</td>
		<td>
			<div id="class_2_signed"></div>
		</td>
		<td>
			<div id="class_3_signed"></div>
		</td>
		<td>
			<div id="class_4_signed"></div>
		</td>
		<td>
			<div id="class_5_signed"></div>
		</td>
		<td>
			<div id="class_6_signed"></div>
		</td>
		<td>
			<div id="class_7_signed"></div>
		</td>
		<td>
			<div id="class_8_signed"></div>
		</td>
		<td>
			<div id="class_9_signed"></div>
		</td>
		<td>
			<div id="class_10_signed"></div>
		</td>		
	</tr>
</table>

<div style="clear: both"></div>
<h1>Unsigned:</h1>
<table class="shaded" style="width : 100%"> 
	<tr>
		<th>Death Knight</th>
		<th>Druid</th>
		<th>Hunter</th>
		<th>Mage</th>
		<th>Paladin</th>
		<th>Priest</th>
		<th>Rogue</th>
		<th>Shaman</th>
		<th>Warlock</th>
		<th>Warrior</th>		
	</tr>
	<tr>
		<td>
			<div id="class_1_unsigned"></div>
		</td>
		<td>
			<div id="class_2_unsigned"></div>
		</td>
		<td>
			<div id="class_3_unsigned"></div>
		</td>
		<td>
			<div id="class_4_unsigned"></div>
		</td>
		<td>
			<div id="class_5_unsigned"></div>
		</td>
		<td>
			<div id="class_6_unsigned"></div>
		</td>
		<td>
			<div id="class_7_unsigned"></div>
		</td>
		<td>
			<div id="class_8_unsigned"></div>
		</td>
		<td>
			<div id="class_9_unsigned"></div>
		</td>
		<td>
			<div id="class_10_unsigned"></div>
		</td>		
	</tr>
</table>

<form name="roles" action="<?= $this->url(array('controller'=>'raids', 'action'=>'signup')) ?>"  method="post">

<h1>Characters:</h1>
<table class="shaded">
	<tr>
		<th>Name</th>
		<th>Class</th>
		<th>Main</th>
		<th>Role</th>
		<th>note</th>
		<th>Sign-up</th>
	</tr>
<?php if(count($this->characters)) : ?>
<?php foreach ($this->characters as $character) : ?>
<tr class="<?= $this->alternator(array("odd","even")); ?>">
	<td><?= $character->name;?></td>
	<td><?= $character->class_id['name'];?></td>
	<td><?php if ($character->main == '1') echo "Main"; else echo "Alt";?></td>
	<td><?php echo $this->roles[$character->id];?></td> <!--  id = 'char' . $character->id . 'roles' -->
	<td><input type="textarea" name="char<?= $character->id ?>note" value="" /></td>
	<td>
		<div id="signup_<?=$character->id?>" STYLE="cursor: pointer" onclick="signup(<?=$character->id?>);">Sign-up</div>
		<div id="unsign_<?=$character->id?>" STYLE="cursor: pointer" onclick="unsign(<?=$character->id?>);">Unsign</div>
	</td>
</tr>
<?php endforeach; ?>
<?php else : ?>
<tr>
	<td colspan='2'>No Characters</td>
</tr>
<?php endif; ?>
</table>

<input type="button" name="test" onclick="refreshSignups();" value="Refresh Signups" />

</form>


<!-- 

<form name="roles" action="<?= $this->url(array('controller'=>'raids', 'action'=>'execute')) ?>"  method="get">
<input type="submit" name="raidId" value="<?=$this->raidId?>" />
</form>
 -->
